package com.blue.jdbc.config;

import com.blue.jdbc.dialect.MySQLDialect;
import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.w3c.dom.Element;

/**
 * TransactionManager解析
 *
 * @author Jin Zheng
 * @date 2018-10-31
 */
public class TransactionManagerParser extends AbstractSimpleBeanDefinitionParser
{
	public TransactionManagerParser()
	{
	}

	@Override
	protected Class<?> getBeanClass(Element element)
	{
		return DataSourceTransactionManager.class;
	}

	@Override
	protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder)
	{
		try
		{;
			String dataSource = element.getAttribute("data-source");
			builder.addPropertyValue("dataSource", new RuntimeBeanReference(dataSource));
		}
		catch (Exception e)
		{
			parserContext.getReaderContext().error(MySQLDialect.class.getName() + " 无法创建", element, null, e);
		}
	}
}
